package org.quartz.watch.dc.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerUtils;
import org.quartz.watch.QWContext;
import org.quartz.watch.dc.QWJobDialogController;

public class QWJobDC<R extends QWJobDC.JobRecord<?>> extends QWAbstractDC<R>  implements QWJobDialogController<R> {
	private JobRecord<QWJobDC<?>> jobRecord;
	List<JobRecord<QWJobDC<?>>> jobList;
	
	@Override
	public int getRecordCount() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<R> getRecords() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean navigationActionPerformed() {
		jobRecord = null;
		jobList = null; // force refresh job list
		return true;
	}

	public String saveJobActionPerformed() throws SchedulerException {
		try {
			getJobRecord().getJob().setJobDataMap(new JobDataMap());
			//boolean replace = true;
			//QWContext.getInstance().getSchedulerDialogController().getScheduler().addJob(getJobRecord().getJob(), replace);
			Calendar date = Calendar.getInstance();
			date.add(Calendar.SECOND, 5);
			System.out.println("[JOB SCHEDULED AT] "+date.getTime());

			Trigger trigger = TriggerUtils.makeSecondlyTrigger();
			trigger.setStartTime(date.getTime());
			trigger.setName("myTrigger-"+date.getTime());

			QWContext.getInstance().getSchedulerDialogController().getScheduler().scheduleJob(getJobRecord().getJob(), trigger);
			
			jobRecord = null;
			jobList = null; // force refresh job list
		}
		catch(Exception e) {
			e.printStackTrace();
		}
		return "no_op";
	}

	public JobRecord<QWJobDC<?>> getJobRecord() {
		if(jobRecord == null) {
			jobRecord = new JobRecord<QWJobDC<?>>(this, new JobDetail());
			jobRecord.setRecordController(this);
		}

		return jobRecord;
	}
	
	
	public List<JobRecord<QWJobDC<?>>> getJobList() throws SchedulerException {
		if(jobList == null) {
			jobList = new ArrayList<JobRecord<QWJobDC<?>>>();
			Scheduler scheduler  = QWContext.getInstance().getSchedulerDialogController().getScheduler();
			
			for(String group : scheduler.getJobGroupNames()) {
				for(String name : scheduler.getJobNames(group)) {
					JobRecord<QWJobDC<?>> jr = new JobRecord<QWJobDC<?>>(this, scheduler.getJobDetail(name, group));
					jobList.add(jr);					
				}
			}
		}
		return jobList;
	}

	public static class JobRecord<C extends QWJobDC<?>> extends Record<C> {
		private JobDetail job;

		public JobRecord(C dc, JobDetail job) {
			super(dc);
			this.job = job;
		}

		public JobDetail getJob() {
			return job;
		}

		public String getJobClassName() {
			return job.getJobClass() != null ? job.getJobClass().getCanonicalName() : null;
		}

		public void setJobClassName(String className) throws ClassNotFoundException {
			if(className == null || className.trim().length() == 0) {
				job.setJobClass(null);
			}

			job.setJobClass(Class.forName(className));
		}
	}
}
